package campeonatofutebol.activity;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import campeonatofutebol.bean.RepositorioDeDados;
import campeonatofutebol.persistencia.DBAdapter;
import campeonatofutebol.util.Constantes;

import com.example.campeonatofutebol.R;

public class FeedsListAdapter extends BaseExpandableListAdapter {
	private Context myContext;
	private ArrayList<String> groups;
	private ArrayList<ArrayList<String>> children;
	private Integer[]imgId = {R.drawable.atletico, R.drawable.atlpr, R.drawable.avai,
			R.drawable.bahia, R.drawable.botafogo, R.drawable.corinthians, R.drawable.coritiba,
			R.drawable.cruzeiro, R.drawable.figue, R.drawable.fla, R.drawable.fluminense,
			R.drawable.goias, R.drawable.gremio, R.drawable.interrs, R.drawable.nautico,
			R.drawable.palmeiras, R.drawable.parana, R.drawable.portuguesa, R.drawable.santos,
			R.drawable.sport, R.drawable.saopaulo, R.drawable.treze, R.drawable.vasco, R.drawable.vitoria};
	private Integer[]imgCampeonatos = {R.drawable.noticias, R.drawable.classificacao};
	private ImageView imgView;
	private DBAdapter dbAdapter;

	public FeedsListAdapter(Context context, ArrayList<ArrayList<String>> children ) {
		this.groups = RepositorioDeDados.getInstance().getGrupos();
		this.myContext = context;
		this.children = children; //repositorio.getTimes();
		this.dbAdapter = new DBAdapter(context);
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return children.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		//Time time = (Time) getChild(groupPosition, childPosition);
		final String nome = (String) getChild(groupPosition, childPosition);
		if (convertView == null) {
			LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.child_layout, null);
		}

		TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvChild);
		imgView = (ImageView) convertView.findViewById(R.id.ImageView01);
		final ImageView rightImgView = (ImageView) convertView.findViewById(R.id.ImageView02);
		String grupo = getGroupName(groupPosition);
		if(grupo.equals("Times")){
			//rightImgView.setVisibility(View.INVISIBLE);
			final int favoritosIndex = 4;
			rightImgView.setVisibility(View.VISIBLE);
			imgView.setImageResource(imgId[childPosition]);
			dbAdapter.open();
			if(dbAdapter.timeJaFavoritado(nome)){
				rightImgView.setImageResource(R.drawable.favorito);
				rightImgView.setTag(R.drawable.favorito);
			}else{
				rightImgView.setImageResource(R.drawable.favorito_off);
				rightImgView.setTag(R.drawable.favorito_off);
			}
			
			rightImgView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Object tag = rightImgView.getTag();
					int id = tag == null ? -1 : (Integer) tag;
					dbAdapter.open();
					if(id == R.drawable.favorito ){
						rightImgView.setImageResource(R.drawable.favorito_off);
						rightImgView.setTag(R.drawable.favorito_off);
						int timeFavoritoIndex = dbAdapter.getTimesFavoritados().indexOf(nome);
						children.get(favoritosIndex).remove(timeFavoritoIndex);
						dbAdapter.deletaTimeFavorito(nome);
						notifyDataSetChanged();
					}else{
						rightImgView.setImageResource(R.drawable.favorito);
						rightImgView.setTag(R.drawable.favorito);
						dbAdapter.addTime(nome);
						children.set(favoritosIndex, (ArrayList<String>) dbAdapter.getTimesFavoritados());
					}
					
					dbAdapter.close();
					
				}
			});
			
			dbAdapter.close();
		}else if (grupo.equals("Favoritos")){
			imgView.setImageResource(R.drawable.favorito);
			rightImgView.setVisibility(View.INVISIBLE);
		}else if(grupo.equals("Jogos da Semana")){
			rightImgView.setVisibility(View.INVISIBLE);
			imgView.setImageResource(R.drawable.jogos);
		}else if(grupo.equals("Notificações")){
			rightImgView.setVisibility(View.INVISIBLE);
			imgView.setImageResource(R.drawable.configuracoes);
		}else if(grupo.equals(Constantes.BRAS_SERIEC)){
			rightImgView.setVisibility(View.INVISIBLE);
			imgView.setImageResource(R.drawable.noticias);
		}else{
			rightImgView.setVisibility(View.INVISIBLE);
			imgView.setImageResource(imgCampeonatos[childPosition]);
		}
		//tvPlayerName.setText(" " + time.getNome());
		tvPlayerName.setText(" " + nome);

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		 return children.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		 return groups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		 return groups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		 return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		String group = (String) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.group_layout, null);
		}

		TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvGroup);
		tvGroupName.setText(group);

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
	public String getGroupName(int groupPosition){
		String group = (String) getGroup(groupPosition);
		return group;
	}

}
